package com.sinaapp.mrblog.game.Assasin.model;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.sinaapp.mrblog.game.Assasin.stage.GameStage;
import com.sinaapp.mrblog.game.Assasin.util.AssasinManager;

public class Bullet extends BaseActor {

	public static final int BULLET_STATE_FLYING = 0;
	public static final int BULLET_STATE_DYING = 1;
	public static final int BULLET_STATE_DIED = 2;

	public static final float BULLET_VELOCITY = 20;
	public static final float BULLET_WIDTH = 1f;
	public static final float BULLET_HEIGHT = 1f;
	public static final int BULLET_HURT = 10;

	private Policeman policeman;


	public Bullet(Policeman policeman) {		
		super(policeman.getCenterX(), policeman.getCenterY(), BULLET_WIDTH, BULLET_HEIGHT);
		setState(BULLET_STATE_FLYING);
		this.policeman = policeman;
		angle = MathUtils.atan2(GameStage.killer.getCenterY()
				- getCenterY(), GameStage.killer.getCenterX() - getCenterX())
				*MathUtils.radiansToDegrees;
		System.out.println(getX()+","+getY());
//		setPosition(getX()-minSide/2*MathUtils.cosDeg(angle), getY()-minSide/2*MathUtils.sinDeg(angle));
		setPosition(getX()-minSide/2+minSide/2*MathUtils.cosDeg(angle), getY()-minSide/2+minSide/2*MathUtils.sinDeg(angle));
		collideDecimal = 0.4f;
	}

	@Override
	public void act(float delta) {
		if (bounds.overlaps(GameStage.killer.bounds)&&
				GameStage.killer.life>0) {
			GameStage.listener.shooted(this);
			
			setState(BULLET_STATE_DIED);
		}
		
		region = AssasinManager.bulletRegion;
//		if(getState() == BULLET_STATE_FLYING){
//			region = AssasinManager.bulletRegion;
//		}else (getState() == BULLET_STATE_){
//			
//		}
		vel.set(BULLET_VELOCITY*MathUtils.cosDeg(angle),
				BULLET_VELOCITY*MathUtils.sinDeg(angle));
		vel.scl(delta);
		super.act(delta);
	}

	@Override
	public void moveImpededX() {
		BulletDust dust = null;
		if(vel.x>0){
			dust = new BulletDust(getCenterX()-BULLET_WIDTH/2, getCenterY()-BULLET_HEIGHT/2,0 );
		}else {
			dust = new BulletDust(getCenterX()-BULLET_WIDTH/2, getCenterY()-BULLET_HEIGHT/2,180 );
		}
		GameStage.listener.bulletDust(dust);
		setState(BULLET_STATE_DIED);
		super.moveImpededX();
	}

	@Override
	public void moveImpededY() {
		BulletDust dust = null;
		if(vel.y>0){
			dust = new BulletDust(getCenterX()-BULLET_WIDTH/2, getCenterY()-BULLET_HEIGHT/2,90 );
		}else {
			dust = new BulletDust(getCenterX()-BULLET_WIDTH/2, getCenterY()-BULLET_HEIGHT/2,-90 );
		}
		GameStage.listener.bulletDust(dust);
		setState(BULLET_STATE_DIED);
		super.moveImpededY();
	}

	@Override
	public void refreshStatus() {
		setState(BULLET_STATE_DIED);
	}

	@Override
	public TextureRegion getReadyRegion() {
		return null;
	}


}
